package com.xt.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {

    //创建集合容器对象
    static ArrayList<Student> list = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    public static void main(String[] args,Scanner sc) {


        ol:
        while (true) {
            //1.搭建主界面
            System.out.println("------------------欢迎来到学生管理系统----------------------");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查看学生");
            System.out.println("5. 退出");
            System.out.println("------------------请输入您的选项：----------------------");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list,sc);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list,sc);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent(list,sc);
                    break;
                case "4":
                    queryStudent(list);
                    //System.out.println("查看学生");
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break ol;
                default:
                    System.out.println("您输入的有误");
                    break;
            }
        }
    }

    public static void updateStudent(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入您要修改的学生学号");
        String updateSid = sc.next();
        int index = getIndex(list, updateSid);
        if (index == -1) {
            System.out.println("查无信息，请重新输入");
        } else {
            Student stu = list.get(index);
            System.out.println("信息如下姓名：" + stu.getName() + "，年龄：" + stu.getAge() + "，生日:" + stu.getBirthday());
            System.out.println("请输入您的姓名:");
            String name = sc.next();
            System.out.println("请输入您的年龄:");
            int age = sc.nextInt();
            System.out.println("请输入您的生日:");
            String birthday = sc.next();
            Student st = new Student(updateSid, name, age, birthday);
            list.set(index, st);
            System.out.println("修改成功");
        }

    }

    //查询学生queryStudent
    public static void queryStudent(ArrayList<Student> list) {
        //判断结合中是否有数据
        if (list.size() == 0) {
            System.out.println("无信息，请添加后重新查询");
            return;
        }
        System.out.println("学号\t 姓名 \t 年龄 \t\t 生日");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "岁\t" + s.getBirthday());
        }
    }

    //添加学生的方法
    public static void addStudent(ArrayList<Student> list, Scanner sc) {
        //给出要录入的提示信息
        String sid;
        while (true) {
            System.out.println("请输入您的学号:");
            sid = sc.next();
            //学号不能重复
            int index = getIndex(list, sid);
            if (index == -1) {
                //学号可以使用
                break;
            }
        }
        System.out.println("请输入您的姓名:");
        String name = sc.next();
        System.out.println("请输入您的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入您的生日:");
        String birthday = sc.next();
        //将数据封装到student对象
        Student stu = new Student(sid, name, age, birthday);
        list.add(stu);
        System.out.println("添加成功！");

    }

    //查询学号在集合中的索引
    public static int getIndex(ArrayList<Student> list, String sid) {
        //假设传入的学号不存在,不在集合中
        int index = -1;
        //遍历找到每一个学生对象
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            //获取学生sid
            String id = s.getSid();
            if (id.equals(sid)) {
                //存在，让index变量记录正确的索引位置；
                index = i;
            }
        }
        return index;
    }


    public static void deleteStudent(ArrayList<Student> list, Scanner sc) {
        //给出要录入的提示信息

        System.out.println("请输入您的学号:");
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index == -1) {
            System.out.println("查无信息，重新录入。");
        } else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }
}
